services:
  {{ service_name | default('postgres') }}:
    image: docker.io/library/postgres:17.7
    container_name: {{ container_name | default('postgres') }}
    environment:
      - POSTGRES_INITDB_ARGS={{ postgres_initdb_args | default('--data-checksums') }}
      {% if postgres_host_auth_method %}
      - POSTGRES_HOST_AUTH_METHOD={{ postgres_host_auth_method }}
      {% endif %}
      - POSTGRES_USER={{ database_user | default('postgres') }}
      {% if postgres_secrets_enabled %}
      - POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password
      {% else %}
      - POSTGRES_PASSWORD={{ database_password | default('postgres') }}
      {% endif %}
      - POSTGRES_DB={{ database_name | default('postgres') }}
      - TZ={{ container_timezone | default('UTC') }}
    {% if ports_enabled %}
    ports:
      - "{{ database_port | default(5432) }}:5432"
    {% endif %}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U {{ database_user | default('postgres') }}"]
      start_period: 30s
      interval: 10s
      timeout: 10s
      retries: 5
    {% if network_enabled %}
    networks:
      - {{ network_name | default('bridge') }}
    {% endif %}
    {% if postgres_secrets_enabled %}
    secrets:
      - postgres_password
    {% endif %}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    restart: {{ restart_policy | default('unless-stopped') }}

{% if postgres_secrets_enabled %}
secrets:
  postgres_password:
    file: secret.postgres_password.txt
{% endif %}

volumes:
  postgres_data:
    driver: local

{% if network_enabled %}
networks:
  {{ network_name | default('bridge') }}:
    {% if network_external %}
    external: true
    {% endif %}
{% endif %}
